package org.laobai.generation.utils;

import com.sun.rowset.JdbcRowSetImpl;

import javax.sql.rowset.JdbcRowSet;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class JDBCUtil {

    public static String[] findTableByDatabase(String url, String username, String password) throws Exception {
        PreparedStatement statement = null;
        ResultSet rs = null;
        Connection conn = null;
        List<String> list = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://" + url + "?serverTimezone=UTC", username, password);
            statement = null;
            rs = null;
            list = new ArrayList<>();

            statement = conn.prepareStatement("SHOW DATABASES");
            rs = statement.executeQuery();
            while (rs.next()) {
                list.add(rs.getString(1));
            }
        } catch (Exception e) {
           throw e;
        } finally {
            StreamUtil.close(statement, rs, conn);
        }

        String[] result = new String[list.size()];
        list.toArray(result);
        return result;
    }


}

//
//            con = DriverManager.getConnection("jdbc:mysql://" + url + "?serverTimezone=UTC", username, password);